import 'package:PollOnLine/utils/logger_utils.dart';
import 'package:PollOnLine/values/MyColors.dart';
import 'package:PollOnLine/widget/draggable_floating_action_button.dart';
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:webview_flutter/webview_flutter.dart';

import '../../../main.dart';
import 'add_poll_logic.dart';

/// 添加问卷
class AddPollPage extends StatefulWidget {
  final String title;

  const AddPollPage({Key? key, required this.title}) : super(key: key);

  @override
  State<AddPollPage> createState() => _AddPollPageState();
}

class _AddPollPageState extends State<AddPollPage> {
  final logic = Get.put(AddPollLogic());
  final state = Get.find<AddPollLogic>().state;

  final GlobalKey _parentKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: false,
      onPopInvoked: (bool didPop) async {
        // 当 canPop 为 false，则执行系统返回时会被拦截，并且调用 onPopInvoked 方法，
        // 同时 didPop 为 false， 此时进行逻辑判断，
        // 如果需要返回则执行 Navigator.of(context).pop();
        // 注意此时 onPopInvoked 又会被调用，并且 didPop 为 true。
        if (!didPop) {
          kLog.i(await logic.wbController.currentUrl());
          // if (await logic.wbController.currentUrl() == state.url) {
          if ((await logic.wbController.currentUrl())!.contains(state.url)) {
            Get.back();
          } else {
            logic.wbController.goBack();
          }
        }
      },
      child: GetBuilder<AddPollLogic>(builder: (logic) {
        return GetBuilder<AddPollLogic>(builder: (logic) {
          return Scaffold(
            backgroundColor: MyColors.FFEDF1F5,
            appBar: BrnAppBar(title: "新增样本", themeData: XxxConfigUtils.defaultAppBarConfig),
            body: Stack(
              key: _parentKey,
              children: [
                Column(
                  children: [
                    Visibility(
                      visible: state.isPosition,
                      child: BrnNoticeBar(
                        content: state.location?.address ?? '',
                        showRightIcon: false,
                        marquee: false,
                        leftWidget: Icon(Icons.location_on, size: 16.r, color: MyColors.FF666666),
                        textColor: MyColors.FF333333,
                      ),
                    ),
                    Expanded(
                      child: WebViewWidget(controller: logic.wbController),
                    )
                  ],
                ),
                DraggableFloatingActionButton(
                  initialOffset: Offset(50.r, 50.r),
                  parentKey: _parentKey,
                  onPressed: () {},
                  child: Visibility(
                    visible: state.isRecord,
                    child: Container(
                      width: 50.r,
                      height: 50.r,
                      decoration: BoxDecoration(
                        color: MyColors.FFE9961F,
                        borderRadius: BorderRadius.circular(25.r),
                        boxShadow: [
                          BoxShadow(color: MyColors.B8B3B3B3, blurRadius: 1.r),
                        ],
                      ),
                      child: InkWell(
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Icon(Icons.mic_outlined, color: Colors.white, size: 20.r),
                            Text(state.time, style: TextStyle(fontSize: 12.r, color: Colors.white)),
                            // Text("录音中", style: TextStyle(fontSize: 12.r, color: Colors.white)),
                          ],
                        ),
                        onTap: () {
                          // logic.completePoll();
                        },
                      ),
                    ),
                  ),
                )
              ],
            ),
            // floatingActionButton: IconButton(
            //   onPressed: () {
            //     logic.goto();
            //   },
            //   icon: const Icon(Icons.add),
            // ),
          );
        });
      }),
    );
  }

  @override
  void dispose() {
    Get.delete<AddPollLogic>();
    super.dispose();
  }
}
